package com.hengpeng.msls.controller;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.gson.Gson;
import com.hengpeng.common.exception.ServiceResult;
import com.hengpeng.common.util.StringHelper;
import com.hengpeng.itfinbase.persist.ReceivePlan;
import com.hengpeng.itfinbase.persist.ReceivePlan.ReceiveStatus;
import com.hengpeng.itfinbase.persist.UserInfo;
import com.hengpeng.msls.vo.SessionMember;
/**
 * <pre>
 * 回款控制器
 * </pre>
 * @author wanghao
 * @version 1.0, 2015年11月09日
 */
@Controller
@RequestMapping("/receive")
public class ReceivePlanController extends BaseController
{
	
    /**
     * 回款列表页
     */
    @RequestMapping(value="/receivelist")
    public String listPage(HttpServletRequest request,ModelMap map,Integer type){
        SessionMember session = getSessionMember(request);
        if (session == null) {
            return "/user/login/index.jsp";
        }
        map.addAttribute("type", type);
        return "/page/account/loan/receiveplan.jsp";
    }
    
	/**
	 * <pre>
	 * 收款列表
	 * </pre>
	 * @param map
	 * @param request
	 * @param status 还款计划状态
	 * @param beginDate 还款截止时间查询开始时间
	 * @param endDate 还款截止时间查询结束时间
	 * @param borrowUserName 借款人姓名
	 * @return
	 */
	@RequestMapping(value = "/list")
	public void myReceivePlanList(ModelMap map, HttpServletRequest request ,PrintWriter printWriter,String curPage,Integer type, String startTime,String endTime)
	{
	    Map<String , List<?>> dataMap = new HashMap<String, List<?>>();
        List<Integer> pagecount = new ArrayList<Integer>();
		SessionMember session = getSessionMember(request);
		UserInfo userInfo = session.getUser();
		int int_curPage = (!StringHelper.isEmpty(curPage) ? Integer.parseInt(curPage) : 1);
		ReceiveStatus receiveStatus = ReceiveStatus.WAITREC;
		if(type == 2){
		    receiveStatus = ReceiveStatus.RECEIVED;
		}
        ServiceResult<List<ReceivePlan>> result = serviceFactory.getRepaymentService().getReceivePlansList(null,
                userInfo.getId(), null,receiveStatus,int_curPage,10);
        pagecount.add(result.getReturnListSize());
        dataMap.put("list", result.getReturnValue());
        dataMap.put("pageCount", pagecount);
        Gson gson = new Gson();
        printWriter.write(gson.toJson(dataMap)); 
        printWriter.flush(); 
        printWriter.close();
	}
}
